package com.wkbb.basic.controller.open;

import com.wkbb.basic.model.Grade;
import com.wkbb.basic.model.Organization;
import com.wkbb.basic.service.GradeService;
import com.wkbb.basic.service.OrganizationService;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.ClassInfoDto;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.enums.OrganizationTypeEnum;
import com.wkbb.common.enums.OriginTypeEnum;
import com.wkbb.common.utils.GradeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lilin
 */
@RestController
@Api(description = "年级管理(后台管理)")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class GradeController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private GradeService gradeService;

    @ApiOperation(value = "年级")
    @RequestMapping(value = "getGrade", method = RequestMethod.GET)
    public ResultDTO<GradeDto> getGrade(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "年级ID", required = true) @RequestParam(name = "gradeId", required = true) Long gradeId) {
        log.info("***********GradeController.getGrade******apiVersion:{},gradeId:{}", apiVersion, gradeId);

        try {
            if(null==gradeId)
            {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }

            GradeDto gradeDto = null;
            Grade grade = gradeService.getByPrimaryKey(gradeId);
            if(null!=grade)
            {
                gradeDto = new GradeDto();
                BeanUtils.copyProperties(grade, gradeDto);
            }

            return new ResultDTO<>(gradeDto);
        } catch (Exception e) {
            log.error("***********GradeController.getGrade******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "年级列表")
    @RequestMapping(value = "getGradeInfoList", method = RequestMethod.GET)
    public ResultDTO<List<GradeDto>> getGradeInfoList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "年级类型(1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科 8完中 9九年一贯 10十二年一贯)", required = true) @RequestParam(name = "gradeType", required = true) Integer gradeType)
    {
        log.info("***********GradeController.getGradeInfoList******orgId:{},gradeType:{}", gradeType);

        try {
            List<GradeDto> gradeDtoList = new ArrayList<GradeDto>();
            Map<String, Object> params = new HashMap<>(1);
            params.put("gradeType", gradeType);
            List<Grade> gradeList = gradeService.getListByMapParams(params);
            if(!gradeList.isEmpty())
            {
                for(Grade grade : gradeList) {
                    GradeDto gradeDto = new GradeDto();
                    BeanUtils.copyProperties(grade, gradeDto);

                    gradeDtoList.add(gradeDto);
                }
            }
            return new ResultDTO<>(gradeDtoList);
        } catch (Exception e) {
            log.error("***********GradeController.getGradeInfoList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "年级列表")
    @RequestMapping(value = "getGradeList", method = RequestMethod.GET)
    public ResultDTO<List<GradeDto>> getGradeList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = false) @RequestParam(name = "orgId", required = false) Long orgId,
            @ApiParam(value = "年级类型(1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科 8完中 9九年一贯 10十二年一贯)", required = false) @RequestParam(name = "gradeType", required = false) Integer gradeType) {

        log.info("***********GradeController.getGradeList******orgId:{},gradeType:{}", orgId, gradeType);
        try {
            List<GradeDto> gradeDtoList = new ArrayList<GradeDto>();
            //先查学校有没自定义，没有再根据年级类型查
            Map<String, Object> orgParams = new HashMap<>(1);
            List<Grade> gradeList;
//            orgParams.put("orgId", orgId);
//            orgParams.put("gradeType", gradeType);
            gradeList = gradeService.getListByMapParams(orgParams);
//            if(gradeList.isEmpty() && orgId != null && null==gradeType) {
//                log.info("根据组织查询");
//                Organization organization = organizationService.getByPrimaryKey(orgId);
//                gradeType = GradeUtil.getGradeType(organization.getOrgType()).intValue();
//                Map<String, Object> gradeParams = new HashMap<>();
//                gradeParams.put("gradeType", gradeType);
//                gradeList = gradeService.getListByMapParams(gradeParams);
//            }
            int max=55,min=40;
            if (gradeType!=null && gradeType == 1){
                max = 48;
            }
            if(!gradeList.isEmpty())
            {
                for(Grade grade : gradeList) {
                    GradeDto gradeDto = new GradeDto();
                    BeanUtils.copyProperties(grade, gradeDto);
                    if(gradeDto.getId()>=min && gradeDto.getId()<=max)
                    {
                        gradeDtoList.add(gradeDto);
                    }
                }


            }
            return new ResultDTO<>(gradeDtoList);
        } catch (Exception e) {
            log.error("***********GradeController.getGradeList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据名称查询年级")
    @RequestMapping(value = "/getGradeByName", method = RequestMethod.GET)
    public ResultDTO<GradeDto> getGradeByName(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级名称", required = true) @RequestParam(name = "gradeName", required = true) String gradeName) {

        log.info("***********getGradeByName******orgId:{},className:{}", orgId, gradeName);

        try {

            Map<String, Object> params = new HashMap<>();
            Organization organization = organizationService.getByPrimaryKey(orgId);
            if(organization.getOrigin().equals(OriginTypeEnum.SELF_SUPPORT.getValue()))
            {
                params.put("gradeType", GradeUtil.getGradeType(organization.getOrgType()));
                params.put("gradeName", gradeName);
            }
            else {
                params.put("orgId", orgId);
                params.put("gradeName", gradeName);
            }

            Grade grade = gradeService.getByMapParams(params);
            if(null!=grade) {
                GradeDto gradeDto = new GradeDto();
                BeanUtils.copyProperties(grade, gradeDto);
                return new ResultDTO<GradeDto>(gradeDto);
            }
            return new ResultDTO<GradeDto>(true);
        } catch (Exception e) {
            log.error("***********getGradeByName******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

}
